package chapters16;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by xinxuchu on 2020/1/31.
 */
public class WaitThread  extends Thread{

    private volatile boolean fire = false;
    private Lock lock = new ReentrantLock();//显示锁
    private Condition condition = lock.newCondition();//显示条件

    @Override
    public void run(){
        try {
            lock.lock();
            try {
                while (!fire){
                    condition.await();
                }
            }finally {
                lock.unlock();
            }
            System.out.println("fired!");

        }catch (InterruptedException e){
            Thread.interrupted();//中断
        }
    }

    public void fire(){
        lock.lock();
        try {
            this.fire = true;
            condition.signal();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        WaitThread waitThread = new WaitThread();
        waitThread.start();
        Thread.sleep(1000);
        System.out.println("fire");
        waitThread.fire();

    }
}
